/**
 * Created by Administrator on 2016/3/16 0016.
 */
angular.module('app.device', ['ui.router', 'angularUtils.directives.dirPagination'])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider//market
            .state('home', {
                'abstract': true,
                url: '',
                views: {
                    '': {
                        templateUrl: "view/home.html"
                    }
                }
            }).state('home.first', {
                'abstract': true,
                url: '/home',
                views: {
                    'topbar': {
                        templateUrl: 'view/topbar.html',
                        controller: 'TopbarController'
                    }, 'main': {
                        templateUrl: 'view/home/market/sidebar.html',
                        controller: 'HomebaseController'
                    }
                }
            })
            .state('home.first.market', {
                url: '/market',
                templateUrl: 'view/home/market/market.html',
                controller: 'MarketController'
            }).state('home.first.marketdetail', {
                url: '/market/:id',
                templateUrl: 'view/home/market/market.detail.html',
                controller: 'MarketDetailController'
            }).state('home.first.marketadd', {
                url: '/marketadd',
                templateUrl: 'view/home/market/market.add.html',
                controller: 'MarketAddController'
            })
    }])

    //controller
    .controller("MarketController", ['$scope', 'MarketService', function ($scope, MarketService) {
        MarketService.getbyall().$promise.then(function (data) {
            $scope.markets = data;
            //console.log($scope.devices);
        });
        $scope.currentPage = 1;
        $scope.pageSize = 8;

    }]).controller("MarketDetailController", ['$scope', '$stateParams', 'MarketService', function ($scope, $stateParams, MarketService) {
        //console.log($stateParams.devid);
        MarketService.getbyid($stateParams.id).$promise.then(function (data) {
            $scope.market = data;
        });


    }]).controller("MarketAddController", ['$scope', '$state', '$stateParams','MarketService', function ($scope, $state, $stateParams, MarketService) {
        $scope.market={};
        $scope.market.mOnlinetime='';
        $scope.market.mOfflinetime='';
        $('#datetimepicker').datetimepicker({
            format: "yyyy-MM-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.market.mOnlinetime = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $('#datetimepicker1').datetimepicker({
            format: "yyyy-MM-dd hh:ii",
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left"
        }).on('changeDate', function (ev) {
            $scope.market.mOfflinetime = ev.date.valueOf();
            //console.log(ev.date.valueOf());
        });
        $scope.add = function (market) {
            $().ready(function() {
                $("#commentForm").validate({
                    submitHandler:function(){
                        MarketService.add(market, function (data) {
                            alert("添加商超成功！");
                            $state.go("home.first.market");
                        }, function (data) {
                            alert("添加商超失败，请重试！");
                        })
                    }
                });
            });
        }
    }])
